﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

using Game.Global.Helper;

namespace Game
{
    /// <summary>
    /// 用于类的辅助保存和读取少量数据，需实例化，且赋予唯一id以存取对应内容
    /// </summary>
	public class PreferencesHelper 
	{
        const string C_SettingFolder = "ClassPreferences";

        /// <summary>
        /// 类的名作为存储的文件名
        /// </summary>
        private string m_settingFileName;

        /// <summary>
        /// 是否一旦有值改变就立即保存
        /// </summary>
        bool m_saveInstantly;

        /// <summary>
        /// 获得的键值对
        /// </summary>
        Dictionary<string, string> m_dictPrefers;

        //---------------------------------------------------------------------------
        //				Public area
        //---------------------------------------------------------------------------
        #region Public area

        /// <summary>
        /// 构造函数：从类构造对应的Helper
        /// </summary>
        /// <param name="type">服务的类对象</param>
        /// <param name="saveInstantly">是否一旦更改值就立即保存数据，如果设置为false，则需要显式调用Save（）保存</param>
        public PreferencesHelper(System.Type type ,bool saveInstantly )
        {
            m_settingFileName = type.ToString();
            m_saveInstantly = saveInstantly;

            Init();
        }

        [System.Obsolete("Unrecommended.")]
        /// <summary>
        /// 构造函数：直接指定类的id
        /// </summary>
        /// <param name="classId"></param>
        public PreferencesHelper(string classId)
        {
            m_settingFileName = classId;

            Init();
        }
        
        /// <summary>
        /// 获得某个键值的string值，无则返回""
        /// </summary>
        public string GetString(string name)
        {
            if (m_dictPrefers.ContainsKey(name))
            {
                return m_dictPrefers[name];
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 获得某个键值的int值，无则返回0
        /// </summary>
        public int GetInt(string name)
        {
            string val = GetString(name);
            int result;
            int.TryParse(val, out result);
            
            return result;
        }

        /// <summary>
        /// 获得某个键值的bool值，无则返回指定的默认值
        /// </summary>
        /// <param name="defaultVal">在无法获取该值时，指定返回的默认值</param>
        public bool GetBool(string name,bool defaultVal = false)
        {
            string val = GetString(name);
            bool result;

            //--能成功获取时，返回
            if (bool.TryParse(val, out result))
            {
                return result;
            }
            else
            {
                //--否则，则返回指定的默认值
                return defaultVal;
            }
        }

        /// <summary>
        /// 获得某个键值的Float值，无则返回0
        /// </summary>
        public float GetFloat(string name)
        {
            string val = GetString(name);

            //Debug.LogWarning("GET str:"+val);
            float result;
            float.TryParse(val, out result);
            return result;
        }

        //public T GetObj<T>(string name)
        //{ }

        public void SetVal(string name, string val)
        {
            m_dictPrefers[name] = val;

            //--如果指明了立即保存，则一旦有值更改，则保存到文件中
            if (m_saveInstantly)
            {
                Save();
            }
        }

        public void SetVal(string name, int val)
        {
            SetVal(name, val.ToString());
        }

        public void SetVal(string name, float val)
        {
            SetVal(name, val.ToString());
        }

        public void SetVal(string name, bool val)
        {
            SetVal(name, val.ToString());
        }

        //public void SetObj(string name, object val)
        //{ }

        /// <summary>
        /// 保存所有的值
        /// </summary>
        public void Save()
        {
            FileHelper.SaveObjToFile(C_SettingFolder, m_settingFileName, m_dictPrefers);
        }


		#endregion

		//---------------------------------------------------------------------------
		//				Private area
		//---------------------------------------------------------------------------
		#region Private area

        private void Init()
        {
            //有则读入，无则新建PlayerPrefers
            if (FileHelper.FileExist(C_SettingFolder, m_settingFileName))
            {
                #region 读入数据
                m_dictPrefers = FileHelper.ReadFile<Dictionary<string,string>>(C_SettingFolder, m_settingFileName);
                #endregion
            }
            
            //--Still empty
            if(m_dictPrefers ==null)
            {
                m_dictPrefers = new Dictionary<string, string>();
            }

            //
        }

		#endregion
	}
}